In[1]:= SeedRandom[1];
	
nParticles= 50;
	
initPositions[]:=RandomReal[{-10,10},nParticles];
initSpeeds[]:=RandomChoice[{-1,1},nParticles]/1000;(* Random initial directions *)
	
colors=RandomColor[nParticles];
	
bounce[position_,speed_,leftSpeed_,rightSpeed_]:=
  	    If[Abs[position]>10,
   	        -speed,
   	        If[position<0,leftSpeed,rightSpeed]
   	    ];
	
(* Initialize lists to store positions and times *)
positionLists=Table[{},nParticles];
timeList={};
prevPositions=initPositions[];
coolDownPeriod=0.01;(* Cool down period *)
lastReversalTime=ConstantArray[0,nParticles];(* Initialize lastReversalTime *)
	
(* Fixed reversalProbability *)
reversalProbability=0.5;
	
Manipulate[
    (* Update variables when nParticles changes *)
    If[nParticles!=Length[positions],
        nParticles=Length[positions];
        colors=RandomColor[nParticles];
        positions=Take[initPositions[],nParticles];
        speeds=Take[initSpeeds[],nParticles];
        positionLists=Table[{},nParticles];
        prevPositions=positions;
        lastReversalTime=ConstantArray[0,nParticles];
    ];
    
    If[time!=prevTime&&time<=maxTime,
        dt=time-prevTime;
        positions=positions+speeds*dt;
        (* Ensure particles stay within -10 and  + 10 *)
        positions=Clip[positions,{-10,10}];
        speeds=MapThread[bounce,{positions,speeds,ConstantArray[leftSpeed,nParticles],ConstantArray[rightSpeed,nParticles]}];
        (* Add probabilistic reversal of direction *)
        reversalDecisions=RandomChoice[{1-reversalProbability,reversalProbability}->{1,-1},nParticles];
        speeds*=reversalDecisions;
        (* Add probabilistic reversal at position 0 with cool down period *)
        Do[
    If[
        Sign[prevPositions[[i]]]!=Sign[positions[[i]]]&&time>lastReversalTime[[i]]+coolDownPeriod,(* Check if cool down period has passed *)
        If[
            speeds[[i]]>0&&RandomReal[]<RevProbLtoR||speeds[[i]]<0&&RandomReal[]<RevProbRtoL,
            speeds[[i]]=-speeds[[i]];
            lastReversalTime[[i]]=time(* Update lastReversalTime *)
        ]
    ],
    {i,nParticles}
];
        prevPositions=positions;
        prevTime=time;
    
        (* Store positions and time *)
        positionLists=MapThread[Append,{positionLists,positions}];
        AppendTo[timeList,time];
      ];
    If[time==maxTime,
        (* Plot positions versus time when simulation ends *)
        nRight=Length@Select[positions,#>0&];(* Particles on right *)
        nLeft=Length@Select[positions,#<0&];(* Particles on left *)
    
        Grid[{
              {ListLinePlot[Transpose[{timeList,#}]&/@positionLists,Joined->True,PlotRange->All,PlotStyle->colors,AxesLabel->{"Time","Position"},ImageSize->Large]},
              {Text[Style["Particles on the Right(+): "<>ToString[nRight],Medium]],Text[Style["Particles on the Left(-): "<>ToString[nLeft],Medium]]}
            },Frame->All]
        ,
        Graphics[
            {Thread[{colors,PointSize[Large],Point[{#,0}]&/@positions}],InfiniteLine[{0,0},{0,1}]},
            PlotRange->{{-11,11},{-1,1}},
            AspectRatio->1/8
          ]
      ],
    {time,0,maxTime,.01,ContinuousAction->True},
    {{nParticles,100},1,200,1},
    {{leftSpeed,.01},.001,.1,.001},(* New control for left particle speed with updated range *)
    {{rightSpeed,.01},.001,.1,.001},(* New control for right particle speed with updated range *)
    {{RevProbLtoR,0.5},0,1,.01},(* New control for reversal probability at position 0 for left to right *)
    {{RevProbRtoL,0.5},0,1,.01},(* New control for reversal probability at position 0 for right to left *)
    {{positions,initPositions[]},None},
    {{speeds,initSpeeds[]},None},
    {{prevTime,0},None},
    {{maxTime,6000},None},
    Button["Reset",positions=initPositions[];speeds=initSpeeds[];prevTime=0;time=0;positionLists=Table[{},nParticles];timeList={};prevPositions=initPositions[];lastReversalTime=ConstantArray[0,nParticles];],
    TrackedSymbols:>{time}

(*INSTRUCTIONS:
   set reversal probabilities and speed for the particles on the left and right of the midline. 

Then drag the 'time' slider toward the right. Drag slower for longer trial duration. 

Particle trajectories will be shown along with the number of particles accumulating on the left half and the right half
	
  Click 'Reset' to run a new trial
*)
]